

define(['vue','config','common'],function(Vue,config,utils){
	'use strict';

	var alterPhoneView=new Vue({
		el:'#app',
		data:{
			phone:'',
			phoneDisplay:'',
			phoneCode:'',
			newPhone:'',
			phoneCode2:'',
			secondOfDelay : 120000 ,
			secondLeftOfDelay : 0 
		},
		created:function(){	
			utils.userToken();
			this.typeName = utils.getData('typeName');
			var _this = this;
			utils.ajax(utils.getWebUrl('user/personal'),null,'POST',function(res){
				//console.log(res)
				_this.phone = res.data.phone;
				_this.phoneDisplay = utils.showPhone(res.data.phone);
			},function(){
				$('.DataLoading').show();
			},function(){
				$('.DataLoading').hide();
			})
		},
		methods:{
			toalterPhoneTwo:function(){
				if($.trim(this.phoneCode) === ""){
					utils.appAlert('验证码不能为空');
					return;
				}
				var _this = this;
				utils.ajax(utils.getWebUrl('user/updateRegPhone1'),{
					phoneCode:_this.phoneCode
				},'POST',function(res){
					window.location.href="alterPhoneTwo.html";
				})
			},
			saveNewPhone:function(){
				if($.trim(this.newPhone) === ""){
					utils.appAlert('手机号码不能为空');
					return;
				}
				if(!(/^1(3|4|5|7|8)\d{9}$/.test(this.newPhone))){
                    utils.appAlert('请输入正确的手机号码');
                    return;
				}
				if(this.newPhone === this.phone){
                    utils.appAlert('新手机号码不能与原手机号码相同');
                    return;
				}
				if($.trim(this.phoneCode2) === ""){
					utils.appAlert('验证码不能为空');
                    return;
				}
				var _this = this;
				utils.ajax(utils.getWebUrl('user/updateRegPhone2'),{
					phone:_this.newPhone,
					phoneCode:_this.phoneCode2
				},'POST',function(res){
					 utils.appAlert('修改成功,请重新登录',function(){
					 	//清除所有本地存储
	            		utils.clearData()
			        	window.location.href="login.html";
			        });
				})
			},
			getVeryCode:function(codeType){
				var phone;
				var _this=this;
				if(window.location.href.match("alterPhoneTwo.html")){
					phone = _this.newPhone;
					if($.trim(this.newPhone) === ""){
						utils.appAlert('手机号码不能为空');
						return;
					}
					if(!(/^1(3|4|5|7|8)\d{9}$/.test(this.newPhone))){
	                    utils.appAlert('请输入正确的手机号码');
	                    return;
					}
					if(this.newPhone === this.phone){
	                    utils.appAlert('新手机号码不能与原手机号码相同');
	                    return;
					}
				}else{
					phone = _this.phone
				}

				var data = {
					phone: phone,
					codeType: codeType,
					smsType: 1
				};
				utils.initAjax('verify/sendPhoneCode',data,'POST',function(res){
			        utils.appAlert('请查收你的短信',function(){
			        	_this.sendVerifyCodeTimer();
			        });
			        $(".verification").attr('disabled','true');
				})
			},
			getvoiceMessage:function(codeType){

				var _this = this;
				var data = {
					phone: _this.phone,
					codeType: codeType,
					smsType: 2
				};
				utils.initAjax('verify/sendPhoneCode',data,'POST',function(res){
			        utils.appAlert('你正在使用语音获取验证码<br/>请保持手机通畅');
				})
			},
			sendVerifyCodeTimer:function(){
				var $time_debounce = $('.verification');
				var _this=this;
				_this.setTimeToDefaut = function(secondsOfInterval) {
                	this.secondLeftOfDelay = secondsOfInterval;
            	};

				var secondsOfInterval = this.secondOfDelay;
				var intervalID = setInterval(function() {
	                secondsOfInterval = secondsOfInterval - 1000;
	                $time_debounce.html(" " + secondsOfInterval / 1000 + " 秒 ")
	                if (secondsOfInterval <= 0) {
	                    clearInterval(intervalID);
	                    $(".verification").removeAttr("disabled");
	                    $time_debounce.html("获取验证码");
	                }
	                _this.setTimeToDefaut(secondsOfInterval);
	            }, 1000);
			}
		}
	})

	return alterPhoneView
});